package edu.spbu.eclipse.addressbook.andrpodk;

import java.util.*;
import java.awt.Dimension;

public class Editor{

	public int addNew(ArrayList<Entry> addressBook, Entry e){
		int size = addressBook.size();
		Entry e1;
		addressBook.add(e);
		int i;
		for ( i = size; i > 0; i--){
			if ( addressBook.get(i).getName().compareToIgnoreCase
					( addressBook.get(i-1).getName()) < 0 ){
				e1 = addressBook.get(i);
				addressBook.set(i, addressBook.get(i-1));
				addressBook.set(i-1, e1);
			}
			else {
				break;
			}
		}				
		return i;
	}

	public void delete(ArrayList<Entry> addressBook, int elementIndex){		
		addressBook.remove(elementIndex);		
	}

	public Dimension change(ArrayList<Entry> addressBook, Entry e1, Entry e2){		
		int i=addressBook.indexOf(e1);
		if(e1.getName().equals(e2.getName())){
			addressBook.set(i, e2);
			return new Dimension(i,i);
		}else{
			delete(addressBook, i);
			int j=addNew(addressBook, e2);					
			return new Dimension(i,j);
		}		
	}
}
